package cn.geminis.workflow.service.service.engine.listener;

import cn.geminis.workflow.service.service.engine.listener.event.TaskCreatedEvent;
import lombok.RequiredArgsConstructor;
import org.flowable.common.engine.api.delegate.event.FlowableEntityEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.common.engine.impl.cfg.TransactionState;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.stereotype.Service;

/**
 * @author puddi
 */
@Service
@RequiredArgsConstructor
public class TaskCreatedListener implements FlowableEventListener {

    private final AbstractApplicationContext context;

    @Override
    public void onEvent(FlowableEvent flowableEvent) {
        var task = (TaskEntity) ((FlowableEntityEvent) flowableEvent).getEntity();
        context.publishEvent(new TaskCreatedEvent(task));
    }

    @Override
    public boolean isFailOnException() {
        return true;
    }

    @Override
    public boolean isFireOnTransactionLifecycleEvent() {
        return true;
    }

    @Override
    public String getOnTransaction() {
        return TransactionState.COMMITTED.name();
    }
}
